package com.lagou.edu.service.impl;

import com.lagou.edu.dao.ResumeResipotry;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import com.lagou.edu.utils.MD5Utils;
import com.lagou.edu.vo.LoginVo;
import com.lagou.edu.vo.ResultInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: ResumeService
 * @Auther: Jerry
 * @Date: 2020/7/19 13:15
 * @Desctiption: ResumeService接口
 * @Version: 1.0
 */
@Service
@Transactional
public class ResumeServiceImpl implements ResumeService {

    @Autowired
    private ResumeResipotry resumeResipotry;

    @Override
    public List<Resume> findAll() throws Exception {
        return resumeResipotry.findAll();
    }

    @Override
    public Page<Resume> findByPage(Integer pageNo, Integer pageSize) throws Exception {
        System.out.println("pageNo:" + pageNo + ",pageSize:" +pageSize);
        return resumeResipotry.findAll(PageRequest.of(pageNo, pageSize));
    }

    @Override
    public boolean save(Resume resume) throws Exception {
        if (!StringUtils.isBlank(resume.getPassword())) {
            resume.setPassword(MD5Utils.md5(resume.getPassword()));
        }
        Resume resume1 = resumeResipotry.save(resume);
        return resume1 == null ? false : true;
    }

    @Override
    public void delete(Long id) throws Exception {
        resumeResipotry.deleteById(id);
    }

    @Override
    public ResultInfo login(String username, String password) throws Exception {
        if (StringUtils.isBlank(username)){
            return ResultInfo.error("用户名不能为空");
        }
        if (StringUtils.isBlank(password)){
            return ResultInfo.error("密码不能为空");
        }
        Resume resume = resumeResipotry.findByUsername(username);
        if (resume != null) {
            // 密码加密
            String pass = MD5Utils.md5(password);
            if (!pass.equals(resume.getPassword())) {
                return ResultInfo.error("用户名或密码不正确");
            } else {
                return ResultInfo.success(resume);
            }
        } else {
            return ResultInfo.error("该用户不存在~");
        }
    }
}
